In [1]:
import plotly
plotly.offline.init_notebook_mode()
import cufflinks as cf
cf.go_offline()
from plotly.graph_objs import Bar,Layout, Figure,Data,Scattermapbox,Marker,Surface,XAxis,YAxis,ZAxis,Scene,Scatter
In [2]:
import pandas as pd
import numpy as np
In [3]:
trace1 = Scatter(x=[1,2,3], 
                 y=[4,5,6], 
                 marker={'color': 'red', 
                         'symbol': 104, 
                         'size': "10"}, 
                 mode="markers+lines",  
                 text=["one",
                       "two",
                       "three"], 
                 name='1st Trace')
data=Data([trace1])
layout=Layout(title="First Plot", 
              xaxis={'title':'x1'}, 
              yaxis={'title':'x2'})
fig=Figure(data=data,
           layout=layout)
plotly.offline.iplot(fig)
In [4]:
fig
Out[4]:
{'data': [{'marker': {'color': 'red', 'size': '10', 'symbol': 104},
   'mode': 'markers+lines',
   'name': '1st Trace',
   'text': ['one', 'two', 'three'],
   'type': 'scatter',
   'x': [1, 2, 3],
   'y': [4, 5, 6]}],
 'layout': {'title': 'First Plot',
  'xaxis': {'title': 'x1'},
  'yaxis': {'title': 'x2'}}}
In [5]:
fig.update(dict(layout=dict(title='Plot update'), 
                data=dict(marker=dict(color='blue'))))
plotly.offline.iplot(fig)

Life expectancy plot

In [6]:
df = pd.read_csv('https://raw.githubusercontent.com/yankev/test/master/life-expectancy-per-GDP-2007.csv')
In [7]:
americas = df[(df.continent=='Americas')]
europe = df[(df.continent=='Europe')]

trace_comp0 = Scatter(x=americas.gdp_percap,
                      y=americas.life_exp,
                      mode='markers',
                      marker={'size':12,
                              'line':{'width':1},
                              'color':"#4682B4"
                             },
                      name='Americas',
                      text=americas.country,
                     )

trace_comp1 = Scatter(x=europe.gdp_percap,
                      y=europe.life_exp,
                      mode='markers',
                      marker={'size':12,
                              'line':{'width':1},
                              'color':"#FF7F50"
                             },
                      name='Europe',
                      text=europe.country,
                     )

data_comp = [trace_comp0, trace_comp1]
layout_comp = Layout(title='Life Expectancy v. Per Capita GDP, 2007',
                     hovermode='closest',
                     xaxis={'title':'GDP per capita (2000 dollars)',
                            'ticklen':5,
                            'zeroline':False,
                            'gridwidth':2,
                           },
                     yaxis={'title':'Life Expectancy (years)',
                            'ticklen':5,
                            'gridwidth':2,
                           },
                    )
fig = Figure(data=data_comp, 
             layout=layout_comp)
plotly.offline.iplot(fig)
In [8]:
data
Out[8]:
[{'marker': {'color': 'red', 'size': '10', 'symbol': 104},
  'mode': 'markers+lines',
  'name': '1st Trace',
  'text': ['one', 'two', 'three'],
  'type': 'scatter',
  'x': [1, 2, 3],
  'y': [4, 5, 6]}]
In [9]:
s=Scatter(x=[1,2,3], 
          y=[4,5,6], 
          marker={'color': 'red', 
                  'symbol': 104, 
                  'size': "10"}, 
          mode="markers+lines",  
          text=["one",
                "two",
                "three"])
In [10]:
s
Out[10]:
{'marker': {'color': 'red', 'size': '10', 'symbol': 104},
 'mode': 'markers+lines',
 'text': ['one', 'two', 'three'],
 'type': 'scatter',
 'x': [1, 2, 3],
 'y': [4, 5, 6]}
In [11]:
Figure(data=[s])
Out[11]:
{'data': [{'marker': {'color': 'red', 'size': '10', 'symbol': 104},
   'mode': 'markers+lines',
   'text': ['one', 'two', 'three'],
   'type': 'scatter',
   'x': [1, 2, 3],
   'y': [4, 5, 6]}]}
In [12]:
plotly.offline.iplot(Figure(data=[s]))

Data

In [14]:
x = np.arange(1,3.2,0.2)
y = 6*np.sin(x)
y
Out[14]:
array([5.04882591, 5.59223452, 5.91269838, 5.99744162, 5.84308579,
       5.45578456, 4.85097842, 4.05277908, 3.09300823, 2.0099289 ,
       0.84672005])
In [25]:
trace2 = Scatter(x=x, 
                 y=y, 
                 marker={'color': '#2E8B57', 
                         'symbol': 'pentagon', 
                         'size': 10}, 
                 mode='markers', 
                 name='2nd trace')
data = Data([trace1, 
             trace2])
data
Out[25]:
[{'marker': {'color': 'red', 'size': '10', 'symbol': 104},
  'mode': 'markers+lines',
  'name': '1st Trace',
  'text': ['one', 'two', 'three'],
  'type': 'scatter',
  'x': [1, 2, 3],
  'y': [4, 5, 6]},
 {'marker': {'color': '#2E8B57', 'size': 10, 'symbol': 'pentagon'},
  'mode': 'markers',
  'name': '2nd trace',
  'type': 'scatter',
  'x': array([1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4, 2.6, 2.8, 3. ]),
  'y': array([5.04882591, 5.59223452, 5.91269838, 5.99744162, 5.84308579,
         5.45578456, 4.85097842, 4.05277908, 3.09300823, 2.0099289 ,
         0.84672005])}]
In [26]:
plotly.offline.iplot(Figure(data=data,
                            layout=layout))